﻿#region Header
/*
 * name：ResourceProvider
 * author：Kain
 * date：12/2/2010 10:40:56 AM
 *********************************************
 * History:
 * $Header$
 * $History$
 */
#endregion

#region imported namespace
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Compilation;
using System.Resources;
using System.Threading;
#endregion

namespace Janus.Cores.Globalization
{
    /// <summary>
    /// 
    /// </summary>
    internal class ResourceProvider : IResourceProvider,IResourcesProvider
    {
        string _resourceKey;
        static Dictionary<string, LanguageEntry> _dict = new Dictionary<string,LanguageEntry>();
        static ResourceProvider()
        {
            Initailize();
        }

        public ResourceProvider() { }

        public ResourceProvider(string resourceKey)
        {
            _resourceKey = resourceKey;
        }

        private static void Initailize()
        {
             //IDataFactory dbHelper = DataFactory.Create("remp");
             //using (var reader = dbHelper.ExecuteReader(System.Data.CommandType.Text, "Select * From languageentry"))
             //{
             //    while (reader.Read())
             //    {
             //        var entry = LanguageEntry.GetEntry(reader);
             //        _dict.Add(entry.Key, entry);
             //    }
             //}
        }

        #region IResourceProvider Members

        public object GetObject(string resourceKey, System.Globalization.CultureInfo culture)
        {
            culture = culture ?? System.Threading.Thread.CurrentThread.CurrentUICulture;

            LanguageEntry entry;
            if (_dict.TryGetValue(resourceKey, out entry))
            {
                return entry[culture];
            }
            return string.Empty;
        }

        public System.Resources.IResourceReader ResourceReader
        {
            get
            {
                return new ResourceReader(_dict);
            }
        }

        #endregion

        #region IResourcesProvider Members


        public object GetObject(string name)
        {
            return this.GetObject(name, Thread.CurrentThread.CurrentUICulture);
        }

        public string GetString(string name, System.Globalization.CultureInfo culture)
        {
            return GetObject(name, culture).ToString();
        }

        public string GetString(string name)
        {
            return GetObject(name).ToString();
        }

        #endregion


        public IEnumerable<string> Keys {
            get { return _dict.Keys; }
        }
    }
}
